<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>更新客户</title>
		<script type="text/javascript" src="/js/jquery-1.11.1.js"></script>
		<script type="text/javascript" src="/js/bootstrap.js"></script>
		<link rel="stylesheet" type="text/css" href="/css/bootstrap.css"/>
		<link rel="stylesheet" type="text/css" href="/css/style.css"/>
		<style>
			label span{color: #ff0000; padding-right: 5px;}
		</style>
	</head>

	<body>
		<h2>更新客户</h2>
		<form id="myForm" class="form-horizontal">
			
			<div class="form-group">
				<label class="col-sm-1 control-label"><span>*</span>客户编号</label>
				<div class="col-sm-9">
					<input required="required" name="c_id" class="form-control" placeholder="请输入 客户id"/>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-1 control-label"><span>*</span>客户名称</label>
				<div class="col-sm-9">
					<input required="" name="c_name" class="form-control" placeholder="请输入 客户名称"/>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-1 control-label"><span>*</span>客户联系方式1</label>
				<div class="col-sm-9">
					<input required="" name="c_phone1" class="form-control" placeholder="请输 客户联系方式1"/>
				</div>
			</div>

			<div class="form-group">
				<label class="col-sm-1 control-label">客户联系方式2</label>
				<div class="col-sm-9">
					<input name="c_phone2" class="form-control" placeholder="请输 客户联系方式2"/>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-1 control-label">地址</label>
				<div class="col-sm-9">
					<input name="c_address" class="form-control" placeholder="请输入 客户地址"/>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-1 control-label">备注</label>
				<div class="col-sm-9">
					<input name="c_remarks" class="form-control" placeholder="请输入 备注"/>
				</div>
			</div>
			

			<div class="form-group">
				<label class="col-sm-1 control-label"></label>
				<div class="col-sm-9">
					<button id="btnUpdate" type="button" class="btn btn-sm btn-info">提&nbsp;交</button>
				</div>
			</div>
		</form>		
	</body>
</html>
<script>
	String.prototype.GetValue= function(para) {
		let reg = new RegExp("(^|&)"+ para +"=([^&]*)(&|$)");
		let r = this.substr(this.indexOf("\?")+1).match(reg);
		if (r!=null) return unescape(r[2]); return null;
	}
	$(function () {
		//根据地址获取teamId,如果null则执行增加，否则执行更新
		let url = document.location.toString();//获取URL
		let c_num=url.GetValue("c_num");
		//alert(teamId);
		//根据id查询要更新的球队信息并回显到页面
		queryById(c_num);
		//提交按钮的单击事件--实现真正的更新
		$("#btnUpdate").click(function () {
			$.ajax({
				type: "POST",//这里必须写POST
				url: "/client/",
				data: $("#myForm").serialize()+"&_method=PUT"+"&c_num="+c_num,
				dataType:"json",
				success: function(vo){
					if(vo.code==200) {
						alert("操作成功！");
						window.location.href = "/pages/client/list.html";
					}else{
						alert("更新失败！"+vo.msg);
					}
				}
			});
		});
	});
	function queryById(c_num) {
		$.ajax({
			type: "GET",
			url: "/client/"+c_num, //RESTful风格的API定义
			data: "",
			success: function (vo) {
				let obj=vo.obj;
				let c_id=obj.c_id==null?'':obj.c_id;
				let c_name=obj.c_name==null?'':obj.c_name;
				let c_phone1=obj.c_phone1==null?'':obj.c_phone1;
				let c_phone2=obj.c_phone2==null?'':obj.c_phone2;
				let c_address=obj.c_address==null?'':obj.c_address;
				let c_remarks=obj.c_remarks==null?'':obj.c_remarks;
				$("input[name='c_id']").val(c_id);
				$("input[name='c_name']").val(c_name);
				$("input[name='c_phone1']").val(c_phone1);
				$("input[name='c_phone2']").val(c_phone2);
				$("input[name='c_address']").val(c_address);
				$("input[name='c_remarks']").val(c_remarks);
			}
		});
	}

</script>